package com.beordie.controller;

import com.beordie.model.Category;
import com.beordie.model.User;
import com.beordie.service.ICategoryService;
import com.beordie.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * @Description 分类管理控制层
 * @Date 2021/8/4 20:00
 * @Created 30500
 */
@RequestMapping("category")
@Controller
public class CategoryController {
    @Resource
    private ICategoryService categoryServiceImpl;

    @RequestMapping(value = "list")
    public String pageInfo(@RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "10") int pageSize,
                           Model model) {

        PageInfo<Category> pageInfo = categoryServiceImpl.findPage(pageNum, pageSize);
        model.addAttribute("page", pageInfo);
        return "/category/list";
    }

    @RequestMapping(value = "toUpdate/{id}", method = RequestMethod.GET)
    public String tpUpdate(@PathVariable("id") int id, Model model) {
        Category category = categoryServiceImpl.findById(id);
        model.addAttribute("category", category);
        return "/category/update";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(int id, Category category) {
        category.setId(id);
        int update = categoryServiceImpl.update(category);
        return "redirect:/category/list";
    }

    @RequestMapping("toAdd")
    public String toAdd() {
        return "/category/add";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(Category category) {
        int add = categoryServiceImpl.add(category);
        return "redirect:/category/list";
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable("id") int id) {
        int delete = categoryServiceImpl.delete(id);
        return "redirect:/category/list";
    }

}
